package com.fc.util;

import org.locationtech.jts.geom.*;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;
//import org.geotools.geojson.geom.GeometryJSON;

public class GeoProcessor {
    private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory();
    private static final int SRID = 4326; // WGS84 坐标系
    
    // 创建点
    public static Point createPoint(double longitude, double latitude) {
        Coordinate coord = new Coordinate(longitude, latitude);
        Point point = GEOMETRY_FACTORY.createPoint(coord);
        point.setSRID(SRID);
        return point;
    }
    
    // 创建线
    public static LineString createLine(Coordinate[] coordinates) {
        LineString line = GEOMETRY_FACTORY.createLineString(coordinates);
        line.setSRID(SRID);
        return line;
    }
    
    // 创建多边形
    public static Polygon createPolygon(Coordinate[] coordinates) {
        LinearRing ring = GEOMETRY_FACTORY.createLinearRing(coordinates);
        Polygon polygon = GEOMETRY_FACTORY.createPolygon(ring);
        polygon.setSRID(SRID);
        return polygon;
    }
    
    // WKT 字符串转几何对象
    public static Geometry wktToGeometry(String wkt) throws ParseException {
        WKTReader reader = new WKTReader(GEOMETRY_FACTORY);
        Geometry geometry = reader.read(wkt);
        geometry.setSRID(SRID);
        return geometry;
    }
    
    // 几何对象转 WKT 字符串
    public static String geometryToWkt(Geometry geometry) {
        WKTWriter writer = new WKTWriter();
        return writer.write(geometry);
    }


    public static void main(String[] args) throws ParseException {
        Geometry geometry =GeoProcessor.wktToGeometry("POINT(1 2)");
        if (geometry.getGeometryType().equals(Geometry.TYPENAME_POINT)){
            Point point = (Point) geometry;

        }
        System.out.println(geometry);
    }

    
    // 几何对象转 GeoJSON
//    public static String geometryToGeoJson(Geometry geometry) throws IOException {
//        GeometryJSON geoJson = new GeometryJSON();
//        StringWriter writer = new StringWriter();
//        geoJson.write(geometry, writer);
//        return writer.toString();
//    }
//
//    // GeoJSON 转几何对象
//    public static Geometry geoJsonToGeometry(String geoJson) throws IOException {
//        GeometryJSON json = new GeometryJSON();
//        return json.read(geoJson);
//    }
}